<?php

	//get the username
	$username = $_GET['username'];
	$usernameExists = false;
	
	//generate the query and escape it
	$query = sprintf("SELECT username FROM CS275.userlogin WHERE username='%s'", mysql_real_escape_string($username));
	
	//connect to database
	$link = mysql_connect('localhost', 'root', 'admin');
	if(!$link){
		die('Could not connect: ' . mysql_error());	
	}
	
	//perform query
	$result = mysql_query($query, $link);
	
	//if it failed
	if (!$result) {
    	$message  = 'Invalid query: ' . mysql_error() . "\n";
    	$message .= 'Whole query: ' . $query;
    	die($message);
	}
	
	//if we get results
	while($row = mysql_fetch_assoc($result)){
		$usernameExists = true;	
	}
	
	//free results
	mysql_free_result($result);
	
	//close link
	mysql_close($link);
	
	//make false print false, true for true
	$boolarray = Array(false => 'false', true => 'true');
	
	//output json
	echo(json_encode(array("exists" => $boolarray[$usernameExists])));

?>